package com.zuo.service.serviceImpl;

import com.zuo.entity.Staff;
import com.zuo.mapper.EmployeeMapper;
import com.zuo.service.EmployeeService;
import com.zuo.util.DateFormat;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 白白旧维
 * @Date 2022/3/3 10:14
 */
@Service
@Transactional(readOnly = true)
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Staff> listStaff() {
        List<Staff> staffs = employeeMapper.listStaff();
        for (Staff staff:staffs){
            staff.setCreateTime(DateFormat.format(staff.getScreateTime()));
        }
        return staffs;
    }

    @Override
    public List<Staff> searchStaff(Map<String,Object> map) {
        List<Staff> staffs = employeeMapper.searchStaff(map);
        for (Staff staff:staffs){
            staff.setCreateTime(DateFormat.format(staff.getScreateTime()));
        }
        return staffs;
    }

    @Override
    public Staff getOne(int id) {
        return employeeMapper.getOne(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updateStaff(Staff staff) {
        return employeeMapper.updateStaff(staff);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int removeStaffs(String ids) {
        String[] id = ids.split(",");
        int count=0;
        for (String i : id) {
            int sid=0;
            if(StringUtils.isNotBlank(i)){
                sid=Integer.parseInt(i);
            }
            int result = employeeMapper.removeStaff(sid);
            if(result!=0){
                count++;
            }
        }
        if(count!=id.length){
            throw new RuntimeException("删除失败");
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int addStaff(Staff staff) {
        staff.setScreateTime(new Date(System.currentTimeMillis()));
        return employeeMapper.addStaff(staff);
    }

}
